<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2020-2030 衡水山木枝技术服务有限公司
 * @Date: 2021-09-30 08:58:56
 * @Description: 修改个人资料/密码
 * @LastEditors: 李志刚
 * @LastEditTime: 2021-09-30 09:20:48
 * @FilePath: /qingshanERP/app/Http/Controllers/Tenant/TenantController.php
 */

namespace App\Http\Controllers\Tenant;

use App\Http\Controllers\Controller;
use Validator;
use App\Customize\Func;
use App\Models\QingShanERP\Shop;
use Illuminate\Http\Request;

class TenantController extends Controller
{
    // 修改信息
    public function postInfo(Request $request)
    {
        try {
            $tenant_id = $request->tenant->id;
            $shopname = $request->input('shopname','不知道');
            $phone = $request->input('phone', '13000000000');
            $update = ['shopname' => $shopname, 'phone' => $phone];
            Shop::where('id', $tenant_id)->update($update);
            return $this->resData(200, '修改成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '失败，请稍后再试...', $e->getMessage());
        }
    }
    // 查看单条信息
    public function postDetail(Request $request)
    {
        try {
            $tenant_id = $request->tenant->id;
            $tenant = Shop::findOrFail($tenant_id);
            return $this->resData(200, '查询成功...', $tenant);
        } catch (\Throwable $e) {
            return $this->resData(500, '查询失败，请稍后再试...',$e->getMessage());
        }
    }
    // 修改密码
    public function postPwd(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'password' => 'confirmed|min:6|max:15|alpha_dash',
            ]);
            $attrs = array(
                'password' => '密码',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $tenant_id = $request->tenant->id;
            $crypt = Func::str_random(10);
            $pwd = Func::makepwd($request->input('password'), $crypt);
            $data = ['password' => $pwd, 'crypt' => $crypt];
            Shop::where('id', $tenant_id)->update($data);
            return $this->resData(200, '修改成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '失败，请稍后再试...', $e->getMessage());
        }
    }
}
